<?php
/**
 * 为了ajxa做本域名的api
 * @todo 能否使用开放api，用jsonp调用。
 */
class ApiController extends BaseController
{
    public function __construct($uri)
    {
        parent::__construct($uri);
        $this->output['http']['Content-Type'] = 'application/json';
    }

    protected function checkLogin()
    {
        if(isset($_COOKIE['access_token'])) {
            $token = UserModel::decryptToken($_COOKIE['access_token']);
            return $token;
        }
        $this->output['http']['status_code'] = 403;
        throw new HttpException('', 403, $this->output);
    }
    
    public function getNotifications()
    {
        $token = $this->checkLogin();
        $m = new UserModel();
        $user = $m->get($token['user_id']);
        $m = new NotificationModel();
        $r = $m->getLastest($token['user_id']);
        $this->output['data'] = $r;
        $this->output['http']['cache-control'] = array(
            'private',
            'max-age' => 120,
        );
        return $this->output;
    }

    public function countUnreadedNotifications()
    {
        $token = $this->checkLogin();
        $m = new UserModel();
        $user = $m->get($token['user_id']);
        $m = new NotificationModel();
        $r = $m->countUnreaded($token['user_id']);
        $this->output['data'] = array(
            'count' => $r,
        );
        $this->output['http']['cache-control'] = array(
            'no-cache',
        );
        return $this->output;
    }

    public function getUnreadedNotifications()
    {
        $token = $this->checkLogin();
        $m = new UserModel();
        $user = $m->get($token['user_id']);
        $m = new NotificationModel();
        $r = $m->getUnreaded($token['user_id']);
        $this->output['data'] = $r;
        $this->output['http']['cache-control'] = array(
            'private',
            'max-age' => 120,
        );
        return $this->output;
    }

    public function markReadedNotifications()
    {
        $token = $this->checkLogin();
        $m = new UserModel();
        $user = $m->get($token['user_id']);
        $m = new NotificationModel();
        $r = $m->MarkReaded($token['user_id']);
        $this->output['data'] = $r;
        $this->output['http']['cache-control'] = array(
            'no-cache',
        );
        return $this->output;
    }

    public function getMe()
    {
        $token = $this->checkLogin();
        $m = new UserModel();
        $user = $m->get($token['user_id']);
        $m = new BonusModel();
        $r = $m->isTodayLogined($token['user_id']);
        $user['is_today_logined'] = $r===true ? 1 : 0;
        $this->output['data'] = $user;
        $this->output['http']['cache-control'] = array(
            'private',
            'max-age' => 120,
        );
        return $this->output;
    }

    public function addUserAgentLog()
    {
        $token = $this->checkLogin();
        $m = new BonusModel();
        $m->setWebLoginBonus($token['user_id'], $_SERVER['HTTP_USER_AGENT'], $this->input);
        $this->output['http']['cache-control'] = array(
            'no-cache',
        );
        return $this->output;
    }
}
?>
